package cn.com.yncc

import cn.com.callback.ServiceBackInvoke
import cn.com.elite.EliteBean
import cn.com.kh.KHUtil
import net.sf.json.JSONArray
import net.sf.json.JSONObject
import org.apache.commons.logging.Log
import org.apache.commons.logging.LogFactory
import org.springframework.jdbc.core.JdbcTemplate

import javax.servlet.http.HttpServletRequest
import javax.servlet.http.HttpServletResponse


/**
 * @Title:省网短信发送
 * @Description:
 * @Param:
 * @return:
 * @Author: TaoHao
 * @Date: 2023/7/24
 */
public class apiGwMsgSending implements ServiceBackInvoke {

    private static final Log log = LogFactory.getLog(apiGwMsgSending.class);

    @Override
    String doCallBack(EliteBean eliteBean, HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) {

        httpServletResponse.setCharacterEncoding("UTF-8");
        httpServletResponse.setContentType("application/json;charset=UTF-8");
        JdbcTemplate jt = new JdbcTemplate(eliteBean.getDataSource());
        // 初始化返回参数
        JSONObject result = new JSONObject();

        // 获取接口调用方式及参数
        String getMethod = httpServletRequest.getMethod();
        log.debug("[getMethod]:" + getMethod);

        String inData = "";
        if (getMethod.equals("GET")) {
            inData = httpServletRequest.getParameter("data");
            log.debug("[原始Get请求参数]:" + inData);
        } else {
            InputStreamReader reader = new InputStreamReader(httpServletRequest.getInputStream(), "UTF-8");
            BufferedReader iny = new BufferedReader(reader);
            String line;
            while ((line = iny.readLine()) != null) {
                inData += line;
            }
            log.debug("[原始Post请求参数]:" + inData);
        }
        log.info("[调用请求信息]:" + inData);

        //  入参为空
        if (KHUtil.isNull(inData)) {
            log.debug("【国网短信发送接口】入参为空-无法完成操作!");
            result.put("status", -2);
            result.put("msg", "入参为空-无法完成操作!");
            result.put("data", "{}");
            return result.toString();
        }

        try {
            //解析请求的jSON
            JSONObject inJson = JSONObject.fromObject(inData);
            JSONArray dataArr = inJson.getJSONArray("data");//入参请求的数组


            log.debug("【国网短信发送接口】请求数组数量：" +dataArr.size());
            for (int i = 0; i < dataArr.size(); i++) {

                JSONObject T2JSON = JSONObject.fromObject(i);
                String guid = T2JSON.getString("guid");//guid
                String mobile = T2JSON.getString("mobile");//手机号
                String content = T2JSON.getString("content");//短信内容



                //校验guid 是否在之前发送过




            }



        } catch (Exception e) {
            log.error("【国网短信发送接口】数据处理错误:", e);
            result.put("status", -1);
            result.put("msg", "数据处理错误[" + e.getMessage() + "]");
            result.put("data", "{}");
        }


        return result.toString();
    }
}
